package maxygen;

import com.cycling74.max.*;

/**
 * <code>libmaxygen</code> is a subclass of <code>MaxObject</code>.
 * <code>libmaxygen</code> contains references to native code which obtains information
 * about Max objects in your patch that is unavailable directly in the MXJ environment in 
 * Max 4.6.
 * 
 * To generate the C JNI header in terminal:
 * 
 * cd to the root of the project ("/Users/martinrobinson/workspace/MXJ/" on my system) and execute:
 * 
 * javah -jni maxygen.libmaxygen
 * 
 * @author martinrobinson
 */
public class libmaxygen extends MaxObject {

    static {
        /* 
         * library name is "maxygen"
         * on MacOS this is in the file "libmaxygen.jnilib"
         * then this class "libmaxygen.class" is jar'd into "libmaxygen.jar" 
         * both files should go in
         * /Library/Java/extensions (on Mac OS)
         * 
         * NEED TO CHECK THIS INFO AS THERE WERE CLASSPATH PROBLEMS...
         * 
         * we could inspect java.library.path on Windows to see where it looks
         * I think it's C:\WINNT\system32
         * 
         * we may not need to use JNI in Max 5 (who knows?) since the patcher
         * format may result in getting at the text a little easier
        */
        System.loadLibrary("maxygen");
    }
    
    /** @deprecated
     * use StringUtility.generateNameString() instead
     */
    public static String generateNameString(Object thisob)
    {
        return thisob.getClass().getName()+'@'+System.identityHashCode(thisob);
    }
    
    /**
     * Gets the response from an object in response to a "psave" message in Max 4.6. This causes
     * a object to save its state into a binbuf which we return as text here.
     * 
     * @param obname    the name of the object to query
     * @param thisname  the name of the mxj object making the request
     *                  (so we can know which patcher to search in)
     * @return          the binbuf string
     */
    private native String getpsave0(String obname, String thisname);
    
    /**
     * Gets the response from an object in response to a "psave" message in Max 4.6. This causes
     * a object to save its state into a binbuf which we return as text here.
     * 
     * @param thisob    usually the maxygen.engine object that calls this method
     * @param obname    the name of the object to query
     * @return          the binbuf string
     */
    protected String getpsave(MaxObject thisob, String obname)
    {
        String thisname = StringUtility.generateNameString(thisob);
        String patchername = StringUtility.generateNameString(thisob.getParentPatcher());
        String unique = patchername+":"+thisname;
        
        thisob.getMaxBox().setName(unique);
        
        return getpsave0(obname, unique);
    }    
    
    // Max 5 versions
    private native String getpsave50(String obname, String thisname);
    protected String getpsave5(MaxObject thisob, String obname)
    {
        String thisname = StringUtility.generateNameString(thisob);
        String patchername = StringUtility.generateNameString(thisob.getParentPatcher());
        String unique = patchername+":"+thisname;
        
        thisob.getMaxBox().setName(unique);
        
        return getpsave50(obname, unique);
    }
    
}